# Copyright (c) 2011 Antoine d'Otreppe de Bouvette
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import unittest2

from aspyct.injector import singleton, prototype

class SampleClass:
    pass

class InheritedClass(SampleClass):
    pass

class Environment:
    @singleton
    def single(self):
        return SampleClass()

    @prototype
    def alwaysNew(self):
        return SampleClass()

    @prototype
    def alwaysNewInBaseEnv(self):
        return SampleClass()

    @singleton
    def singleInBaseEnv(self):
        return SampleClass()

class InheritedEnv(Environment):
    @prototype
    def alwaysNew(self):
        return InheritedClass()

    @singleton
    def alwaysNewInBase(self):
        return InheritedClass()

    @prototype
    def singleInBaseClass(self):
        return InheritedClass()

class InjectorTest(unittest2.TestCase):
    def setUp(self):
        self.env = Environment()
        self.inh = InheritedEnv()

    def test_worksAsProperties(self):
        obj = self.env.single
        self.assertEqual(obj.__class__, SampleClass,
            "Must be instance of SampleClass")

    def test_worksAsProperties2(self):
        obj = self.env.alwaysNew
        self.assertEqual(obj.__class__, SampleClass,
            "Must be an instance of SampleClass")

    def test_singletonIsAlwaysTheSameObject(self):
        obj1 = self.env.single
        obj2 = self.env.single
        self.assertIs(obj1, obj2, "Must be the same object")

    def test_prototypeIsAlwaysANewObject(self):
        obj1 = self.env.alwaysNew
        obj2 = self.env.alwaysNew
        self.assertIsNot(obj1, obj2, "Must NOT be the same object")

    def test_environmentsInheritProperties(self):
        obj = self.inh.single
        self.assertEqual(obj.__class__, SampleClass,
            "Must be an instance of SampleClass")

    def test_environmentsOverrideProperties(self):
        obj1 = self.inh.alwaysNewInBase
        obj2 = self.inh.alwaysNewInBase
        self.assertIs(obj1, obj2, "Must be the same object")

    def test_environmentsOverrideProperties2(self):
        obj1 = self.inh.singleInBaseClass
        obj2 = self.inh.singleInBaseClass
        self.assertIsNot(obj1, obj2, "Must NOT be the same object")

if __name__ == "__main__":
    unittest2.main()

